package com.lsw.jgyf.ui.activity;

import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/7/10 0010.
 */

public class CustomerInformationPresenter extends BasePresenter<CustomerInformationContract.View> implements CustomerInformationContract.Presenter {

    @Inject
    HttpApi httpApi;

    @Inject
    public CustomerInformationPresenter() {

    }

    @Override
    public void getData() {
        mView.setData();
    }

    @Override
    public void UpBankcard(String url) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .UpBankcard(url)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        mView.setBankcard(baseBean.getData());
                    } else if (baseBean.getStatus() == Constants.CODE_LOGIN) {
                        ToastUtil.showToast(mContext, "您的登录信息已过期，请重新登录！");
                        mView.openlogin();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void UpFrontCard(String front_url) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .upFrontcardImage(front_url)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        mView.setFront(baseBean.getData());
                    } else if (baseBean.getStatus() == Constants.CODE_LOGIN) {
                        ToastUtil.showToast(mContext, "您的登录信息已过期，请重新登录！");
                        mView.openlogin();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void UpBackCard(String number, String back_url) {
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .upBackcardImage(back_url, number)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        mView.setBackCard(baseBean.getData());
                    } else if (baseBean.getStatus() == Constants.CODE_LOGIN) {
                        ToastUtil.showToast(mContext, "您的登录信息已过期，请重新登录！");
                        mView.openlogin();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void CreateOrder(String phone_id, String memory, String color, String num, String is_new, String mobile, String packageX, String price, String periods, String insurance_id, String company_id, String company_product_id, String first_money, String name, String sex, String id_number, String address, String issue_time, String end_time, String issue, String bank_name, String bank_card_number, String buy_type, String discount_price, String package_expenses) {
        if (StringUtil.isNullOrEmpty(name)) {
            ToastUtil.showToast(mContext, "请输入姓名！");
            return;
        }
        if (StringUtil.isNullOrEmpty(sex)) {
            ToastUtil.showToast(mContext, "请选择性别！");
            return;
        }
        if (StringUtil.isNullOrEmpty(id_number)) {
            ToastUtil.showToast(mContext, "请输入身份证号！");
            return;
        }
        if (StringUtil.isNullOrEmpty(address)) {
            ToastUtil.showToast(mContext, "请输入住址！");
            return;
        }
        if (StringUtil.isNullOrEmpty(issue_time)) {
            ToastUtil.showToast(mContext, "请输入身份证有效期限！");
            return;
        }
        if (StringUtil.isNullOrEmpty(issue)) {
            ToastUtil.showToast(mContext, "请输入身份证签发机构！");
            return;
        }
        if (StringUtil.isNullOrEmpty(bank_name)) {
            ToastUtil.showToast(mContext, "请输入开户银行！");
            return;
        }
        if (StringUtil.isNullOrEmpty(bank_card_number)) {
            ToastUtil.showToast(mContext, "请输入银行卡号！");
            return;
        }
        LogUtils.i("lsw", "phone_id=" + phone_id + ",memory=" + memory + ",color=" + color + ",num=" + num + ",is_new=" + is_new + ",mobile=" + mobile + ",packageX=" + packageX +
                ",price=" + price + ",periods=" + periods + ",insurance_id=" + insurance_id + ",company_id=" + company_id + ",company_product_id=" + company_product_id + ",first_money=" + first_money +
                ",name=" + name + ",sex=" + sex + ",id_number=" + id_number + ",address=" + address + ",issue_time=" + issue_time + ",end_time=" + end_time + ",issue=" + issue + ",bank_name+" + bank_name + ",bank_card_number=" + bank_card_number+ ",buy_type=" + buy_type+ ",discount_price=" + discount_price+ ",package_expenses=" + package_expenses);
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .CreateOrder(phone_id, memory, color, num, is_new, mobile, packageX, price, periods, insurance_id, company_id, company_product_id, first_money, name, sex, id_number, address, issue_time, end_time, issue, bank_name, bank_card_number, buy_type, discount_price, package_expenses)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                        mView.CreateOrderSucceed(null);
                    } else if (baseBean.getStatus() == Constants.CODE_LOGIN) {
                        ToastUtil.showToast(mContext, "您的登录信息已过期，请重新登录！");
                        mView.openlogin();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }
}
